import 'package:collection/collection.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/low_code/components/single_form_group_widget.dart';
import 'package:yt_devops/low_code/components/single_form_item_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'low_code_single_form_controller.dart';

class LowCodeSingleFormPage extends GetView<LowCodeSingleFormController> {
  const LowCodeSingleFormPage({Key? key}) : super(key: key);
  get state => Get.find<LowCodeSingleFormController>().state;
  get controllers =>
      List.generate(Get.find<LowCodeSingleFormController>().state.formConfig.groupList.length, (groupIndex) {
        var group = Get.find<LowCodeSingleFormController>().state.formConfig.groupList[groupIndex];
        return group.childrens.map((item) => TextEditingController()).toList();
      });
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          state.title,
          style: TextStyle(fontSize: 17.sp, fontWeight: FontWeight.bold),
        ),
      ),
      body: GetBuilder<LowCodeSingleFormController>(builder: (controller) {
        return GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              // 隐藏键盘
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Container(
              padding: const EdgeInsets.only(top: 15),
              child: ListView(
                children: [
                  SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Wrap(
                      children: List.generate(state.formConfig.groupList.length, ((index) => index)).map((groupIndex) {
                        var group = state.formConfig.groupList[groupIndex];
                        return Column(
                          children: [
                            Singleformgroupwidget(
                                title: group.groupTitle,
                                childrens: List.generate(group.childrens.length, ((index) => index)).map((itemIndex) {
                                  var item = group.childrens[itemIndex];
                                  return GetBuilder<LowCodeSingleFormController>(builder: (controller) {
                                    return SingleformItemwidget(
                                      title: item.title,
                                      type: item.type,
                                      maxValue: item.maxValue,
                                      minValue: item.minValue,
                                      value: item.value ?? '',
                                      hintTextString: item.hintTextString ?? '',
                                      selectList: item.selectList ?? [],
                                      controller: controllers[groupIndex][itemIndex],
                                      rightSlot: item.rightSlot,
                                      leftSlot: item.leftSlot,
                                      isRequired: item.isRequired,
                                      titleWidth: item.titleWidth ?? 150,
                                      isCanEdit: item.isCanEdit ?? true,
                                      maxLines: item.maxLines ?? 3,
                                      suffixIcon: item.suffixIcon,
                                      maxLength: item.maxLength ?? 100,
                                      maxPhotoCount: item.maxPhotoCount ?? 3,
                                      valueSetCallBack: (value) {},
                                    );
                                  });
                                }).toList()),
                            Gaps.vGap10,
                          ],
                        );
                      }).toList(),
                    ),
                  )
                ],
              ),
            ));
      }),
      bottomNavigationBar: Container(
        color: Colour.white,
        margin: EdgeInsets.only(top: 10.h),
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
        child: SafeArea(
          child: YtButton(
            enabled: state.canSubmit,
            text: ResKey.submit.tr,
            onPress: () => controller.confirmFormData(),
          ),
        ),
      ),
    );
  }
}
